package com.nys.cqhttp.message.factory.impl;

import cn.hutool.http.HttpUtil;
import com.nys.cqhttp.constant.CommonConstant;
import com.nys.cqhttp.mapper.*;
import com.nys.cqhttp.message.factory.IMessageHandler;

import com.nys.cqhttp.model.*;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.util.DateUtils;
import com.nys.cqhttp.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * DirectImageUrlMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 二次元
 * @date 2021/12/28 19:53
 */
@Slf4j
@Component
public class WJMessageHandler implements IMessageHandler {
  @Autowired private YuanShenMapper yuanShenMapper;
  @Autowired private YuanShenConfigMapper yuanShenConfigMapper;
  @Autowired private MessageLogMapper messageLogMapper;
  @Autowired private SysUserMapper sysUserMapper;
  @Autowired private UserConfigMapper userConfigMapper;

  //  private static final String TEMP_FILE = "D:/img/jk";
  //  private static final String TEMP_FILE_PATH = "D:/img/";

  private static WJMessageHandler wjMessageHandler;
  private static String at = "[CQ:at,qq=";
  private static String at1 = "]";

  @PostConstruct
  public void init() throws Exception {
    wjMessageHandler = this;
    yuanShenMapper = this.yuanShenMapper;
    sysUserMapper = this.sysUserMapper;
    userConfigMapper = this.userConfigMapper;
    yuanShenConfigMapper = this.yuanShenConfigMapper;
    messageLogMapper = this.messageLogMapper;
  }

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    File file = new File(messagePojo.getValue());
    File[] files = file.listFiles();
    try {
      Random r = new Random();
      MessageParam messageParam = context.getMessageParam();
      if (messageParam.getMessage().contains("原神抽卡")) {
        //        YuanShenExample yuanShenExample1 = new YuanShenExample();
        //        YuanShenExample.Criteria criteria4 = yuanShenExample1.createCriteria();
        //        criteria4.andJqrQqEqualTo(messageParam.getSelfId());
        //        criteria4.andUserQqEqualTo(messageParam.getSenderId());
        //        List<YuanShen> myYuanShens =
        //            wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample1);

        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria1 = sysUserExample.createCriteria();
        criteria1.andJqrQqEqualTo(messageParam.getSelfId());
        criteria1.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
          SysUser sysUser = new SysUser();
          sysUser.setJqrQq(messageParam.getSelfId());
          sysUser.setUserQq(messageParam.getSenderId());
          sysUser.setGold(0);
          wjMessageHandler.sysUserMapper.insertSelective(sysUser);
          return at + messageParam.getSenderId() + at1 + "金币为0,10个金币才能抽卡哦";
        } else {
          SysUser sysUser = sysUsers.get(0);
          Integer num = 1;
          if (messageParam.getMessage().contains("原神抽卡-")) {

            num = Integer.valueOf(messageParam.getMessage().substring(5));
            if (num <= 0) {
              return "抽卡次数必须大于0哦";
            }
          }

          File file1 = new File("");
          if (sysUser.getGold() >= 10 * num) {

            sysUser.setGold(sysUser.getGold() - 10 * num);
            wjMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
            List<String> nameList = new ArrayList<>();

            for (Integer i = 0; i < num; i++) {
              file1 = files[r.nextInt(files.length)];
              String name = file1.getName().substring(0, file1.getName().indexOf("."));
              nameList.add(name);
            }
            String res1 = "";
            for (String name : nameList) {
              if (!name.equals("原神")) {
                YuanShenExample yuanShenExample = new YuanShenExample();
                YuanShenExample.Criteria criteria = yuanShenExample.createCriteria();
                criteria.andJqrQqEqualTo(messageParam.getSelfId());
                criteria.andUserQqEqualTo(messageParam.getSenderId());
                criteria.andNameEqualTo(name);
                List<YuanShen> yuanShens =
                    wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample);
                if (CollectionUtils.isEmpty(yuanShens)) {
                  YuanShen yuanShen = new YuanShen();
                  yuanShen.setJqrQq(messageParam.getSelfId());
                  yuanShen.setUserQq(messageParam.getSenderId());
                  yuanShen.setName(name);
                  yuanShen.setStar(1);
                  wjMessageHandler.yuanShenMapper.insertSelective(yuanShen);
                } else {
                  YuanShen yuanShen = yuanShens.get(0);
                  yuanShen.setStar(yuanShen.getStar() + 1);
                  wjMessageHandler.yuanShenMapper.updateByPrimaryKeySelective(yuanShen);
                }
                res1 += name + " ";
              } else {
                res1 += "未中奖 ";
              }
            }
            if (num > 1) {
              HttpUtil.post(
                  messageParam.getUrl() + "/send_group_forward_msg",
                  "{\n"
                      + "    \"group_id\": \""
                      + messageParam.getGroupId()
                      + "\",\n"
                      + "    \"messages\": [\n"
                      + "        {\n"
                      + "            \"type\": \"node\",\n"
                      + "            \"data\": {\n"
                      + "                \"user_id\": \""
                      + messageParam.getSelfId()
                      + "\",\n"
                      + "                \"nickname\": \"云梦星机器人\",\n"
                      + "                \"content\":\""
                      + messageParam.getSenderId()
                      + "你抽奖的角色如下\"\n"
                      + "            }\n"
                      + "        },\n"
                      + "        {\n"
                      + "            \"type\": \"node\",\n"
                      + "            \"data\": {\n"
                      + "                \"user_id\": \""
                      + messageParam.getSelfId()
                      + "\",\n"
                      + "                \"nickname\": \"云梦星机器人\",\n"
                      + "                \"content\":\""
                      + res1
                      + "\"\n"
                      + "            }\n"
                      + "        }\n"
                      + "    ]\n"
                      + "}");
              res1 = "请查看转发信息查看详情";
            }else{
              res1="";
            }
            YuanShenExample yuanShenExample = new YuanShenExample();
            YuanShenExample.Criteria criteria = yuanShenExample.createCriteria();
            criteria.andJqrQqEqualTo(messageParam.getSelfId());
            criteria.andUserQqEqualTo(messageParam.getSenderId());
            List<YuanShen> yuanShens =
                wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample);
            Integer myLife = 1000;
            Integer myAttack = 100;
            Integer myMilitaryStrength = 2000;
            for (YuanShen myYuanShen : yuanShens) {
              YuanShenConfig yuanShenConfig =
                  wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(myYuanShen.getName());
              myLife += yuanShenConfig.getLife();
              myAttack += yuanShenConfig.getAttack();
              myMilitaryStrength += yuanShenConfig.getMilitaryStrength();
              if (myYuanShen.getStar() > 1) {
                myLife += (yuanShenConfig.getLevelLife() * (myYuanShen.getStar() - 1));
                myAttack += (yuanShenConfig.getLevelAttack() * (myYuanShen.getStar() - 1));
                myMilitaryStrength +=
                    (yuanShenConfig.getLevelMilitaryStrength() * (myYuanShen.getStar() - 1));
              }
            }
            sysUser.setAttack(myAttack);
            sysUser.setLife(myLife);
            sysUser.setMilitaryStrength(myMilitaryStrength);

            wjMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);

            String res =
                at
                    + messageParam.getSenderId()
                    + at1
                    + "抽奖成功消耗"
                    + (10 * num)
                    + ",还剩"
                    + sysUser.getGold()
                    + "个";
            if (num > 1) {
              res += "\n" + res1;
            } else {
              res += String.format(CommonConstant.CQ_IMAGE, "file:///" + file1);
            }
            return res;
          } else {
            return at + messageParam.getSenderId() + at1 + "金币不足,你的金币: " + sysUser.getGold() + "个";
          }
        }
      } else if (messageParam.getMessage().equals("原神")) {
        YuanShenExample yuanShenExample = new YuanShenExample();
        YuanShenExample.Criteria criteria = yuanShenExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<YuanShen> yuanShens = wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample);
        if (CollectionUtils.isEmpty(yuanShens)) {
          return at + messageParam.getSenderId() + at1 + "你还没有角色哦";
        } else {
          String res = "你拥有以下角色哦";
          for (int i = 0; i < yuanShens.size(); i++) {
            YuanShen yuanShen = yuanShens.get(i);
            res += "\\n" + (i + 1) + "." + yuanShen.getName() + " " + yuanShen.getStar() + "星";
          }
          HttpUtil.post(
                  messageParam.getUrl() + "/send_group_forward_msg",
                  "{\n"
                          + "    \"group_id\": \""+messageParam.getGroupId()+"\",\n"
                          + "    \"messages\": [\n"
                          + "        {\n"
                          + "            \"type\": \"node\",\n"
                          + "            \"data\": {\n"
                          + "                \"user_id\": \""+messageParam.getSelfId()+"\",\n"
                          + "                \"nickname\": \"云梦星机器人\",\n"
                          + "                \"content\":\""+messageParam.getSenderId()+"你的角色如下\"\n"
                          + "            }\n"
                          + "        },\n"
                          + "        {\n"
                          + "            \"type\": \"node\",\n"
                          + "            \"data\": {\n"
                          + "                \"user_id\": \""+messageParam.getSelfId()+"\",\n"
                          + "                \"nickname\": \"云梦星机器人\",\n"
                          + "                \"content\":\""+res+"\"\n"
                          + "            }\n"
                          + "        }\n"
                          + "    ]\n"
                          + "}");
          return at + messageParam.getSenderId() + at1 + "请查看转发信息查看详情";
        }
      } else if (messageParam.getMessage().contains("原神-")) {
        String substring = messageParam.getMessage().substring(3);
        YuanShenConfig yuanShenConfig =
            wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(substring);
        return at
            + messageParam.getSenderId()
            + at1
            + "\n角色: "
            + yuanShenConfig.getName()
            + "\n战力: "
            + yuanShenConfig.getMilitaryStrength()
            + "\n生命: "
            + yuanShenConfig.getLife()
            + "\n攻击: "
            + yuanShenConfig.getAttack()
            + "\n战力星级加成: "
            + yuanShenConfig.getLevelMilitaryStrength()
            + "\n生命星级加成: "
            + yuanShenConfig.getLevelLife()
            + "\n攻击星级加成: "
            + yuanShenConfig.getLevelAttack();
      } else if (messageParam.getMessage().equals("战力排行榜")) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());

        sysUserExample.setOrderByClause("military_strength DESC limit 0,20");
        List<SysUser> sysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        String res = "战力排行榜";
        for (int i = 0; i < sysUsers.size(); i++) {
          SysUser sysUser = sysUsers.get(i);
          res +=
              "\n" + (i + 1) + "." + sysUser.getUserQq() + "/" + sysUser.getMilitaryStrength() + "";
        }
        return res;
      } else if (messageParam.getMessage().equals("生命排行榜")) {

        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());

        sysUserExample.setOrderByClause("life DESC limit 0,20");
        List<SysUser> sysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        String res = "生命排行榜";
        for (int i = 0; i < sysUsers.size(); i++) {
          SysUser sysUser = sysUsers.get(i);
          res += "\n" + (i + 1) + "." + sysUser.getUserQq() + "/" + sysUser.getLife();
        }
        return res;
      } else if (messageParam.getMessage().equals("攻击排行榜")) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());

        sysUserExample.setOrderByClause("attack DESC limit 0,20");
        List<SysUser> sysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        String res = "攻击排行榜";
        for (int i = 0; i < sysUsers.size(); i++) {
          SysUser sysUser = sysUsers.get(i);
          res += "\n" + (i + 1) + "." + sysUser.getUserQq() + "/" + sysUser.getAttack();
        }
        return res;

      } else if (messageParam.getMessage().equals("我的")) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
          return "请发送'签到'创建账号!";
        }
        SysUser sysUser = sysUsers.get(0);
        /*Integer myLife = 1000;
        Integer myAttack = 100;
        Integer myMilitaryStrength = 2000;
        YuanShenExample yuanShenExample = new YuanShenExample();
        YuanShenExample.Criteria criteria = yuanShenExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<YuanShen> myYuanShens =
            wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample);*/
        /*for (YuanShen myYuanShen : myYuanShens) {
          YuanShenConfig yuanShenConfig =
              wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(myYuanShen.getName());
          myLife += yuanShenConfig.getLife();
          myAttack += yuanShenConfig.getAttack();
          myMilitaryStrength += yuanShenConfig.getMilitaryStrength();
          if (myYuanShen.getStar() > 1) {
            myLife += (yuanShenConfig.getLevelLife() * (myYuanShen.getStar() - 1));
            myAttack += (yuanShenConfig.getLevelAttack() * (myYuanShen.getStar() - 1));
            myMilitaryStrength +=
                (yuanShenConfig.getLevelMilitaryStrength() * (myYuanShen.getStar() - 1));
          }
        }*/
        return at
            + messageParam.getSenderId()
            + at1
            + "\n战力: "
            + sysUser.getMilitaryStrength()
            + "\n精神值: "
            + sysUser.getSpirit()
            + "\n生命: "
            + sysUser.getLife()
            + "\n攻击: "
            + sysUser.getAttack()
            + "\n防御:"
            + sysUser.getDefense();
      } else if (messageParam.getMessage().contains("抢劫")) {
        String substring =
            messageParam.getMessage().substring(13, messageParam.getMessage().indexOf("]"));
        if (substring.equals(messageParam.getSenderId())) {
          return "不能抢劫自己哦!";
        }
        if (messageParam.getSelfId().equals(messageParam.getSenderId())) {
          return "不能抢劫机器人哦!";
        }
        MessageLogExample messageLogExample = new MessageLogExample();
        MessageLogExample.Criteria messageLogExampleCriteria = messageLogExample.createCriteria();
        messageLogExampleCriteria.andMessageEqualTo(messageParam.getMessage());
        messageLogExampleCriteria.andJqrQqEqualTo(messageParam.getSelfId());
        messageLogExampleCriteria.andUserQqEqualTo(substring);
        messageLogExampleCriteria.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<MessageLog> messageLogs =
            wjMessageHandler.messageLogMapper.selectByExample(messageLogExample);
        if (CollectionUtils.isEmpty(messageLogs)) {

          SysUserExample sysUserExample = new SysUserExample();
          SysUserExample.Criteria criteria3 = sysUserExample.createCriteria();
          criteria3.andJqrQqEqualTo(messageParam.getSelfId());
          criteria3.andUserQqEqualTo(messageParam.getSenderId());
          List<SysUser> mySysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);

          SysUserExample sysUserExample1 = new SysUserExample();
          SysUserExample.Criteria criteria2 = sysUserExample1.createCriteria();
          criteria2.andJqrQqEqualTo(messageParam.getSelfId());
          criteria2.andUserQqEqualTo(substring);
          List<SysUser> enemySysUsers =
              wjMessageHandler.sysUserMapper.selectByExample(sysUserExample1);
          SysUser mySysUser = new SysUser();
          SysUser enemySysUser = new SysUser();
          if (CollectionUtils.isEmpty(mySysUsers)) {
            mySysUser.setJqrQq(messageParam.getSelfId());
            mySysUser.setUserQq(messageParam.getSenderId());
            mySysUser.setGold(0);
            mySysUser.setSpirit(100);
            wjMessageHandler.sysUserMapper.insertSelective(mySysUser);
          } else {
            mySysUser = mySysUsers.get(0);
            if (mySysUser.getGold() <= 0) {
              return at + messageParam.getSenderId() + at1 + "你的金币至少1个才能抢劫哦";
            }
          }
          if (CollectionUtils.isEmpty(enemySysUsers)) {
            enemySysUser.setUserQq(substring);
            enemySysUser.setJqrQq(messageParam.getSelfId());
            enemySysUser.setGold(0);
            enemySysUser.setSpirit(100);
            wjMessageHandler.sysUserMapper.insertSelective(enemySysUser);
          } else {
            enemySysUser = enemySysUsers.get(0);
          }

          YuanShenExample yuanShenExample = new YuanShenExample();
          YuanShenExample.Criteria criteria = yuanShenExample.createCriteria();
          criteria.andJqrQqEqualTo(messageParam.getSelfId());
          criteria.andUserQqEqualTo(messageParam.getSenderId());
          List<YuanShen> myYuanShens =
              wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample);
          YuanShenExample yuanShenExample1 = new YuanShenExample();
          YuanShenExample.Criteria criteria1 = yuanShenExample1.createCriteria();
          criteria1.andJqrQqEqualTo(messageParam.getSelfId());

          criteria1.andUserQqEqualTo(substring);
          List<YuanShen> enemyYuanShens =
              wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample1);

          Integer myLife = 1000;
          Integer enemyLife = 1000;
          Integer myAttack = 100;
          Integer enemyAttack = 100;
          Integer myMilitaryStrength = 2000;
          Integer enemyMilitaryStrength = 2000;
          String res = "";
          if (CollectionUtils.isEmpty(myYuanShens)) {
            if (CollectionUtils.isNotEmpty(enemyYuanShens)) {
              for (YuanShen enemyYuanShen : enemyYuanShens) {
                YuanShenConfig yuanShenConfig =
                    wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(
                        enemyYuanShen.getName());
                enemyLife += yuanShenConfig.getLife();
                enemyAttack += yuanShenConfig.getAttack();
                enemyMilitaryStrength += yuanShenConfig.getMilitaryStrength();
                if (enemyYuanShen.getStar() > 1) {
                  enemyLife += (yuanShenConfig.getLevelLife() * (enemyYuanShen.getStar() - 1));
                  enemyAttack += (yuanShenConfig.getLevelAttack() * (enemyYuanShen.getStar() - 1));
                  enemyMilitaryStrength +=
                      (yuanShenConfig.getLevelMilitaryStrength() * (enemyYuanShen.getStar() - 1));
                }
              }
            }
          } else {
            for (YuanShen myYuanShen : myYuanShens) {
              YuanShenConfig yuanShenConfig =
                  wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(myYuanShen.getName());
              myLife += yuanShenConfig.getLife();
              myAttack += yuanShenConfig.getAttack();
              myMilitaryStrength += yuanShenConfig.getMilitaryStrength();
              if (myYuanShen.getStar() > 1) {
                myLife += (yuanShenConfig.getLevelLife() * (myYuanShen.getStar() - 1));
                myAttack += (yuanShenConfig.getLevelAttack() * (myYuanShen.getStar() - 1));
                myMilitaryStrength +=
                    (yuanShenConfig.getLevelMilitaryStrength() * (myYuanShen.getStar() - 1));
              }
            }
            if (CollectionUtils.isNotEmpty(enemyYuanShens)) {
              for (YuanShen enemyYuanShen : enemyYuanShens) {
                YuanShenConfig yuanShenConfig =
                    wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(
                        enemyYuanShen.getName());
                enemyLife += yuanShenConfig.getLife();
                enemyAttack += yuanShenConfig.getAttack();
                enemyMilitaryStrength += yuanShenConfig.getMilitaryStrength();
                if (enemyYuanShen.getStar() > 1) {
                  enemyLife += (yuanShenConfig.getLevelLife() * (enemyYuanShen.getStar() - 1));
                  enemyAttack += (yuanShenConfig.getLevelAttack() * (enemyYuanShen.getStar() - 1));
                  enemyMilitaryStrength +=
                      (yuanShenConfig.getLevelMilitaryStrength() * (enemyYuanShen.getStar() - 1));
                }
              }
            }
          }
          if (enemyMilitaryStrength<=10000){
            return "当前玩家处于保护状态!";
          }
          res += "你的战力: " + myMilitaryStrength + " 敌方战力: " + enemyMilitaryStrength;
          myLife=(int)(myLife*((mySysUser.getSpirit()*1.0)/100));
          enemyLife=(int)(enemyLife*((enemySysUser.getSpirit()*1.0)/100));
          myAttack=(int)(myAttack*((mySysUser.getSpirit()*1.0)/100));
          enemyAttack=(int)(enemyAttack*((enemySysUser.getSpirit()*1.0)/100));
          for (int i = 1; myLife > 0 && enemyLife > 0; i++) {
            int theMyAttack = r.nextInt(myAttack);
            int theEnemyAttack = r.nextInt(enemyAttack);
            int l = myLife - theEnemyAttack;
            int l2 = enemyLife - theMyAttack;
            if (l > 0) {
              myLife -= theEnemyAttack;
            } else {
              myLife = 0;
              res += "\\n第" + i + "回合\\n你的生命: " + myLife + "敌方生命: " + enemyLife;
              break;
            }
            if (l2 > 0) {
              enemyLife -= theMyAttack;
            } else {
              enemyLife = 0;
              res += "\\n第" + i + "回合\\n你的生命: " + myLife + "敌方生命: " + enemyLife;
              break;
            }
            res += "\\n第" + i + "回合\\n你的生命: " + myLife + "敌方生命: " + enemyLife;
          }
          HttpUtil.post(
                  messageParam.getUrl() + "/send_group_forward_msg",
                  "{\n"
                          + "    \"group_id\": \""+messageParam.getGroupId()+"\",\n"
                          + "    \"messages\": [\n"
                          + "        {\n"
                          + "            \"type\": \"node\",\n"
                          + "            \"data\": {\n"
                          + "                \"user_id\": \""+messageParam.getSelfId()+"\",\n"
                          + "                \"nickname\": \"云梦星机器人\",\n"
                          + "                \"content\":\""+messageParam.getSenderId()+ "抢劫" + substring+"\"\n"
                          + "            }\n"
                          + "        },\n"
                          +"         {\n"
                          + "            \"type\": \"node\",\n"
                          + "            \"data\": {\n"
                          + "                \"user_id\": \""+messageParam.getSelfId()+"\",\n"
                          + "                \"nickname\": \"云梦星机器人\",\n"
                          + "                \"content\":\""+res+"\"\n"
                          + "            }\n"
                          + "        }\n"
                          + "    ]\n"
                          + "}");
          int gold;

          int next = 0;
          if (myLife == 0) {

            gold = mySysUser.getGold() >= 10 ? 10 : mySysUser.getGold();
            next = r.nextInt(gold)+1;
            mySysUser.setGold(mySysUser.getGold() - next);
            enemySysUser.setGold(enemySysUser.getGold() + next);
            res = "\n抢劫失败失去" + next + "敌方剩余" + enemySysUser.getGold() + "金币";
          } else {
            gold = enemySysUser.getGold() >= 20 ? 20 : enemySysUser.getGold();
            if (gold > 0) {
              next = r.nextInt(gold);
            }

            mySysUser.setGold(mySysUser.getGold() + next);
            enemySysUser.setGold(enemySysUser.getGold() - next);
            res = "\n抢劫成功获得" + next + "敌方剩余" + enemySysUser.getGold() + "金币";
          }
          mySysUser.setSpirit(mySysUser.getSpirit()-2<0?0:mySysUser.getSpirit()-2);
          wjMessageHandler.sysUserMapper.updateByPrimaryKeySelective(mySysUser);
          wjMessageHandler.sysUserMapper.updateByPrimaryKeySelective(enemySysUser);
          MessageLog messageLog = new MessageLog();
          messageLog.setMessage(messageParam.getMessage());
          messageLog.setUserQq(substring);
          messageLog.setJqrQq(messageParam.getSelfId());
          messageLog.setSendDate(DateUtils.getNowDate());
          wjMessageHandler.messageLogMapper.insertSelective(messageLog);
          return at + messageParam.getSenderId() + at1 + res;
        } else {
          return at + messageParam.getSenderId() + at1 + "今天" + substring + "已经被抢过了哦";
        }
      }else if (messageParam.getMessage().contains("PK")) {
        String substring =
                messageParam.getMessage().substring(13, messageParam.getMessage().indexOf("]"));
        if (substring.equals(messageParam.getSenderId())) {
          return "不能PK自己哦!";
        }
        if (messageParam.getSelfId().equals(messageParam.getSenderId())) {
          return "不能PK机器人哦!";
        }
          SysUserExample sysUserExample = new SysUserExample();
          SysUserExample.Criteria criteria3 = sysUserExample.createCriteria();
          criteria3.andJqrQqEqualTo(messageParam.getSelfId());
          criteria3.andUserQqEqualTo(messageParam.getSenderId());
          List<SysUser> mySysUsers = wjMessageHandler.sysUserMapper.selectByExample(sysUserExample);

          SysUserExample sysUserExample1 = new SysUserExample();
          SysUserExample.Criteria criteria2 = sysUserExample1.createCriteria();
          criteria2.andJqrQqEqualTo(messageParam.getSelfId());
          criteria2.andUserQqEqualTo(substring);
          List<SysUser> enemySysUsers =
                  wjMessageHandler.sysUserMapper.selectByExample(sysUserExample1);
          SysUser mySysUser = new SysUser();
          SysUser enemySysUser = new SysUser();
          if (CollectionUtils.isEmpty(mySysUsers)) {
            mySysUser.setJqrQq(messageParam.getSelfId());
            mySysUser.setUserQq(messageParam.getSenderId());
            mySysUser.setGold(0);
            wjMessageHandler.sysUserMapper.insertSelective(mySysUser);
          } else {
            mySysUser = mySysUsers.get(0);
          }
          if (CollectionUtils.isEmpty(enemySysUsers)) {
            enemySysUser.setUserQq(substring);
            enemySysUser.setJqrQq(messageParam.getSelfId());
            enemySysUser.setGold(0);
            wjMessageHandler.sysUserMapper.insertSelective(enemySysUser);
          } else {
            enemySysUser = enemySysUsers.get(0);
          }

          YuanShenExample yuanShenExample = new YuanShenExample();
          YuanShenExample.Criteria criteria = yuanShenExample.createCriteria();
          criteria.andJqrQqEqualTo(messageParam.getSelfId());
          criteria.andUserQqEqualTo(messageParam.getSenderId());
          List<YuanShen> myYuanShens =
                  wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample);
          YuanShenExample yuanShenExample1 = new YuanShenExample();
          YuanShenExample.Criteria criteria1 = yuanShenExample1.createCriteria();
          criteria1.andJqrQqEqualTo(messageParam.getSelfId());

          criteria1.andUserQqEqualTo(substring);
          List<YuanShen> enemyYuanShens =
                  wjMessageHandler.yuanShenMapper.selectByExample(yuanShenExample1);

          Integer myLife = 1000;
          Integer enemyLife = 1000;
          Integer myAttack = 100;
          Integer enemyAttack = 100;
          Integer myMilitaryStrength = 2000;
          Integer enemyMilitaryStrength = 2000;
          String res = "";
          if (CollectionUtils.isEmpty(myYuanShens)) {
            if (CollectionUtils.isNotEmpty(enemyYuanShens)) {
              for (YuanShen enemyYuanShen : enemyYuanShens) {
                YuanShenConfig yuanShenConfig =
                        wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(
                                enemyYuanShen.getName());
                enemyLife += yuanShenConfig.getLife();
                enemyAttack += yuanShenConfig.getAttack();
                enemyMilitaryStrength += yuanShenConfig.getMilitaryStrength();
                if (enemyYuanShen.getStar() > 1) {
                  enemyLife += (yuanShenConfig.getLevelLife() * (enemyYuanShen.getStar() - 1));
                  enemyAttack += (yuanShenConfig.getLevelAttack() * (enemyYuanShen.getStar() - 1));
                  enemyMilitaryStrength +=
                          (yuanShenConfig.getLevelMilitaryStrength() * (enemyYuanShen.getStar() - 1));
                }
              }
            }
          } else {
            for (YuanShen myYuanShen : myYuanShens) {
              YuanShenConfig yuanShenConfig =
                      wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(myYuanShen.getName());
              myLife += yuanShenConfig.getLife();
              myAttack += yuanShenConfig.getAttack();
              myMilitaryStrength += yuanShenConfig.getMilitaryStrength();
              if (myYuanShen.getStar() > 1) {
                myLife += (yuanShenConfig.getLevelLife() * (myYuanShen.getStar() - 1));
                myAttack += (yuanShenConfig.getLevelAttack() * (myYuanShen.getStar() - 1));
                myMilitaryStrength +=
                        (yuanShenConfig.getLevelMilitaryStrength() * (myYuanShen.getStar() - 1));
              }
            }
            if (CollectionUtils.isNotEmpty(enemyYuanShens)) {
              for (YuanShen enemyYuanShen : enemyYuanShens) {
                YuanShenConfig yuanShenConfig =
                        wjMessageHandler.yuanShenConfigMapper.selectByPrimaryKey(
                                enemyYuanShen.getName());
                enemyLife += yuanShenConfig.getLife();
                enemyAttack += yuanShenConfig.getAttack();
                enemyMilitaryStrength += yuanShenConfig.getMilitaryStrength();
                if (enemyYuanShen.getStar() > 1) {
                  enemyLife += (yuanShenConfig.getLevelLife() * (enemyYuanShen.getStar() - 1));
                  enemyAttack += (yuanShenConfig.getLevelAttack() * (enemyYuanShen.getStar() - 1));
                  enemyMilitaryStrength +=
                          (yuanShenConfig.getLevelMilitaryStrength() * (enemyYuanShen.getStar() - 1));
                }
              }
            }
          }
          res += "你的战力: " + myMilitaryStrength + " 敌方战力: " + enemyMilitaryStrength;
          myLife=(int)(myLife*((mySysUser.getSpirit()*1.0)/100));
          enemyLife=(int)(enemyLife*((enemySysUser.getSpirit()*1.0)/100));
          myAttack=(int)(myAttack*((mySysUser.getSpirit()*1.0)/100));
          enemyAttack=(int)(enemyAttack*((enemySysUser.getSpirit()*1.0)/100));
          for (int i = 1; myLife > 0 && enemyLife > 0; i++) {
            int theMyAttack = r.nextInt(myAttack);
            int theEnemyAttack = r.nextInt(enemyAttack);
            int l = myLife - theEnemyAttack;
            int l2 = enemyLife - theMyAttack;
            if (l > 0) {
              myLife -= theEnemyAttack;
            } else {
              myLife = 0;
            res += "\\n第" + i + "回合\\n你的生命: " + myLife + "敌方生命: " + enemyLife;
              break;
            }
            if (l2 > 0) {
              enemyLife -= theMyAttack;
            } else {
              enemyLife = 0;
            res += "\\n第" + i + "回合\\n你的生命: " + myLife + "敌方生命: " + enemyLife;
              break;
            }
          res += "\\n第" + i + "回合\\n你的生命: " + myLife + "敌方生命: " + enemyLife;
          }
        String url = messageParam.getUrl() + "/send_group_forward_msg";
          String body ="{\n"
                  + "    \"group_id\": \""+messageParam.getGroupId()+"\",\n"
                  + "    \"messages\": [\n"
                  + "        {\n"
                  + "            \"type\": \"node\",\n"
                  + "            \"data\": {\n"
                  + "                \"user_id\": \""+messageParam.getSelfId()+"\",\n"
                  + "                \"nickname\": \"云梦星机器人\",\n"
                  + "                \"content\":\""+messageParam.getSenderId()+ "PK" + substring+"\"\n"
                  + "            }\n"
                  + "        },\n"
                  + "        {\n"
                  + "            \"type\": \"node\",\n"
                  + "            \"data\": {\n"
                  + "                \"user_id\": \""+messageParam.getSelfId()+"\",\n"
                  + "                \"nickname\": \"云梦星机器人\",\n"
                  + "                \"content\":\""+at+messageParam.getSenderId()+at1+res+"\"\n"
                  + "            }\n"
                  + "        }\n"
                  + "    ]\n"
                  + "}";
          log.info(body);
        log.info(url+"1111111111111");
        String post=HttpUtil.post(
                url,
                body);
        log.info(post+"2222222222");
          return at + messageParam.getSenderId() + at1 + "PK详情请看转发信息";
        }
      return String.format(CommonConstant.CQ_IMAGE, "file:///" + files[r.nextInt(files.length)]);
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }
  }
}
